package com.fwtai.controller.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.service.web.GoodsPromotionService;
import com.fwtai.tool.ToolClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商品活动促销控制层|路由层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-05-15 12:54:16
 * @官网 <url>http://www.fwtai.com</url>
*/
@RestController
@RequestMapping("/goodsPromotion")
public class GoodsPromotionController{

  private final GoodsPromotionService goodsPromotionService;

  public GoodsPromotionController(final GoodsPromotionService goodsPromotionService){
    this.goodsPromotionService = goodsPromotionService;
  }

  /**添加*/
  @PreAuthorize("hasAuthority('activity_btn_add')")
  @PostMapping("/add")
  public void add(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.add(request),response);
  }

  /**行内编辑*/
  @PreAuthorize("hasAuthority('activity_row_edit')")
  @PostMapping("/edit")
  public void edit(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.edit(request),response);
  }

  /**根据id查询对应的数据*/
  @PreAuthorize("hasAuthority('activity_row_queryById')")
  @GetMapping("/queryById")
  public void queryById(final Long id,final Long target,final HttpServletResponse response){
    if(target == null){
      ToolClient.responseJson(goodsPromotionService.queryById(id),response);
    }else {
      ToolClient.responseJson(goodsPromotionService.queryInfo(target),response);
    }
  }

  /**获取全部商品*/
  @PreAuthorize("hasAuthority('activity_row_queryGoods')")
  @GetMapping("/queryGoods")
  public void queryGoods(final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.queryGoods(),response);
  }

  /**删除-单行*/
  @PreAuthorize("hasAuthority('activity_row_delById')")
  @PostMapping("/delById")
  public void delById(final Long id,final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.delById(id),response);
  }

  /**批量删除*/
  @PreAuthorize("hasAuthority('activity_btn_delByKeys')")
  @PostMapping("/delByKeys")//若前端传递{kids:'128,256,512'}则此处可直接使用 final Long[] kids 接收参数的
  public void delByKeys(final String ids,final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.delByKeys(ids),response);
  }

  /**获取数据*/
  @PreAuthorize("hasAuthority('activity_btn_listData')")
  @GetMapping("/listData")
  public void listData(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(goodsPromotionService.listData(new PageFormData(request)),response);
  }
}